package cn.com.cym.gamezao.util;


import cn.com.cym.gamezao.entity.SendRes;
import cn.com.cym.gamezao.entity.Sms;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.apache.tomcat.util.codec.binary.Base64;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


import javax.persistence.EntityManager;
import java.io.IOException;

@Component
@ConfigurationProperties(prefix = "smscode")
@Data
public class SMSUtils {
    private static final Logger log = LoggerFactory.getLogger(SMSUtils.class);
    /*@Value("${smscode.apId}")
    private String apId;
    @Value("${smscode.secretKey}")
    private String secretKey;
    @Value("${smscode.ecName}")
    private String ecName;  // 集团名称
    @Value("${smscode.sign}")
    private String sign; // 网关签名编码
    private String addSerial; // 拓展码 填空
    @Value("${smscode.url}")
    public String url; // 请求url*/

    private String apId;
    private String secretKey;
    private String ecName;  // 集团名称
    private String sign; // 网关签名编码
    private String addSerial; // 拓展码 填空
    public String url; // 请求url


    /**
     * 多用户发送短信信息
     *
     * @param mobiles 手机号逗号分隔
     * @param content 短信内容
     * @return int 返回1表示成功，0表示失败
     * @author xiaoyafeng
     * @date 2021/11/17 17:48
     */
    public static SendRes sendMsg(EntityManager entityManager,Sms sendReq,String mobiles, String content,boolean staticFu) throws IOException {
        //因为此处调用了set方法，需要取消自动事务管理
        Session session = entityManager.unwrap(org.hibernate.Session.class);
        sendReq.setContent(content);
        sendReq.setMobiles(mobiles);
        session.evict(sendReq);

        sendReq.setContent(content);
        sendReq.setMobiles(mobiles);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sendReq.getEcName());
        stringBuffer.append(sendReq.getApId());
        stringBuffer.append(sendReq.getSecretKey());
        stringBuffer.append(sendReq.getMobiles());
        stringBuffer.append(sendReq.getContent());
        stringBuffer.append(sendReq.getSign());
        stringBuffer.append(sendReq.getAddSerial());

        sendReq.setMac(MathUtils.MD5(stringBuffer.toString()).toLowerCase());

        String reqText = JSON.toJSONString(sendReq);

        String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));

        //此处使用hutool工具包httpRequest工具类发送https请求
        System.out.println(1+":"+sendReq.getUrl());
        System.out.println(encode);
        String resStr = HttpRequest.post(sendReq.getUrl())
                .header("contentType", "utf-8")
                .body(encode)
                .execute()
                .body();

        log.info("发送短信结果：" + resStr);

        return JSON.parseObject(resStr, SendRes.class);
    }

    /**
     *
     * @param entityManager
     * @param sendReq
     * @param mobiles
     * @param content
     * @return
     * @throws IOException
     */
    public SendRes sendMsg(EntityManager entityManager, Sms sendReq, String mobiles, String content) throws IOException {
        //因为此处调用了set方法，需要取消自动事务管理，不然会进行自定事务管理，进行update语句执行
        Session session = entityManager.unwrap(org.hibernate.Session.class);
        sendReq.setContent(content);
        sendReq.setMobiles(mobiles);
        session.evict(sendReq);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(sendReq.getEcName());
        stringBuffer.append(sendReq.getApId());
        stringBuffer.append(sendReq.getSecretKey());
        stringBuffer.append(sendReq.getMobiles());
        stringBuffer.append(sendReq.getContent());
        stringBuffer.append(sendReq.getSign());
        stringBuffer.append(sendReq.getAddSerial());

        sendReq.setMac(MathUtils.MD5(stringBuffer.toString()).toLowerCase());

        String reqText = JSON.toJSONString(sendReq);

        String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));

        //此处使用hutool工具包httpRequest工具类发送https请求
        String resStr = HttpRequest.post(sendReq.getUrl())
                .header("contentType", "utf-8")
                .body(encode)
                .execute()
                .body();
        log.info("发送短信结果：" + resStr);


        return JSON.parseObject(resStr, SendRes.class);
    }

}
